package com.zlkj.pro.modular.admin.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.admin.model.dto.MaterialGroupAdminDTO;
import com.zlkj.pro.modular.admin.model.vo.MaterialGroupAdminVO;
import com.zlkj.pro.modular.admin.service.MaterialGroupAdminService;
import com.zlkj.pro.modular.common.entity.MaterialGroupEntity;
import com.zlkj.pro.modular.common.mapper.MaterialGroupMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 孙铭泽
 * @date 2022/9/24
 * @time 14:28
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MaterialGroupAdminServiceImpl extends ServiceImpl<MaterialGroupMapper, MaterialGroupEntity> implements MaterialGroupAdminService {

    @Override
    public PageResult<MaterialGroupAdminVO> findPage(MaterialGroupAdminDTO materialGroupAdminDTO) {
        Integer pageNo = materialGroupAdminDTO.getPageNo();
        Integer pageSize = materialGroupAdminDTO.getPageSize();
        LambdaQueryWrapper<MaterialGroupEntity> materialGroupEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<MaterialGroupEntity> materialGroupEntityPage = super.page(new Page<>(pageNo, pageSize), materialGroupEntityLambdaQueryWrapper);
        long total = materialGroupEntityPage.getTotal();
        List<MaterialGroupEntity> records = materialGroupEntityPage.getRecords();
        List<MaterialGroupAdminVO> materialGroupAdminList = ConvertUtil.turn(records, MaterialGroupAdminVO.class);
        return PageResultFactory.createPageResult(materialGroupAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<MaterialGroupAdminVO> findList(MaterialGroupAdminDTO materialGroupAdminDTO) {
        LambdaQueryWrapper<MaterialGroupEntity> materialGroupEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        materialGroupEntityLambdaQueryWrapper.orderByDesc(MaterialGroupEntity::getCreateTime);
        List<MaterialGroupEntity> materialGroupEntityList = super.list(materialGroupEntityLambdaQueryWrapper);
        return ConvertUtil.turn(materialGroupEntityList, MaterialGroupAdminVO.class);
    }

    @Override
    public MaterialGroupAdminVO findDetail(MaterialGroupAdminDTO materialGroupAdminDTO) {
        Long id = materialGroupAdminDTO.getId();
        LambdaQueryWrapper<MaterialGroupEntity> materialGroupEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        materialGroupEntityLambdaQueryWrapper.eq(MaterialGroupEntity::getId, id);
        MaterialGroupEntity materialGroupEntity = super.getOne(materialGroupEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(materialGroupEntity, MaterialGroupAdminVO.class);
    }

    @Override
    public boolean add(MaterialGroupAdminDTO materialGroupAdminDTO) {
        MaterialGroupEntity materialGroupEntity = ConvertUtil.turn(materialGroupAdminDTO, MaterialGroupEntity.class);
        return super.save(materialGroupEntity);
    }

    @Override
    public boolean edit(MaterialGroupAdminDTO materialGroupAdminDTO) {
        MaterialGroupEntity materialGroupEntity = ConvertUtil.turn(materialGroupAdminDTO, MaterialGroupEntity.class);
        return super.updateById(materialGroupEntity);
    }

    @Override
    public boolean delete(MaterialGroupAdminDTO materialGroupAdminDTO) {
        return super.removeById(materialGroupAdminDTO);
    }

}
